//
//  QYXCommentViewController.m
//  OpenYourEyes
//
//  Created by dllo on 16/6/8.
//  Copyright © 2016年 Google AdWords. All rights reserved.
//

#import "QYXCommentViewController.h"
#import "QYXTopRankingModel.h"
#import "QYXCommentTableViewCell.h"
#import "UIImageView+WebCache.h"

@interface QYXCommentViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *commentTableView;

@property (nonatomic, strong) NSMutableArray *commentDataSource;

@property (nonatomic, strong) UIImageView *commentTopImageView;

@property (nonatomic, strong) UIImageView *coverTopImageView;

@property (nonatomic, strong) UIImageView *bottomInforImageView;

@property (nonatomic, strong) UIImageView *commentArrowsImageView;

@property (nonatomic, strong) UILabel *titleLabel;

@property (nonatomic, strong) UILabel *talkLabel;

@property (nonatomic, strong) UILabel *writeLabel;

/** 箭头 */
@property (nonatomic, strong) UIImageView *topArrowImageView;

@end

@implementation QYXCommentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatCommentView];
    [self getCommentData];
 

}



- (void)creatCommentView
{
    //导航栏:
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth / 4, 44)];
    titleLabel.text = @"Eyepelizer";
    titleLabel.textAlignment = 1;
    titleLabel.font = [UIFont fontWithName:@"Lobster 1.4" size:25];
    self.navigationItem.titleView = titleLabel;
    //箭头分栏:
    
    self.topArrowImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"subfield"]];
    UIView *arrowView = [[UIView alloc] initWithFrame:CGRectMake(38, 26, 16, 16)];
    [arrowView addSubview:_topArrowImageView];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:arrowView];
    
    self.topArrowImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer  alloc] initWithTarget:self action:@selector(arrowTuringAction)];
    [self.topArrowImageView addGestureRecognizer:tapGesture];
  
    
    self.commentTopImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 64, kScreenWidth, (kScreenHeight - 64) / 2)];
    self.commentTopImageView.userInteractionEnabled = YES;
    self.bottomInforImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, self.commentTopImageView.bounds.size.height, self.commentTopImageView.bounds.size.width, self.commentTopImageView.bounds.size.height + 49 + 15)];
    self.coverTopImageView = [[UIImageView alloc] initWithFrame:self.bottomInforImageView.frame];
    
    [self.view addSubview:_commentTopImageView];
    [self.view addSubview:_bottomInforImageView];
    [self.view addSubview:_coverTopImageView];
    
    
    for (QYXTopRankingModel *tempModel in self.tempDataArray) {
        
        if (tempModel.id == self.commentId) {
            
            [self.commentTopImageView sd_setImageWithURL:[NSURL URLWithString:tempModel.coverModel.feed] placeholderImage:kPlaceHolderImage completed:nil];
            [self.coverTopImageView sd_setImageWithURL:[NSURL URLWithString:tempModel.coverModel.blurred] placeholderImage:kPlaceHolderImage completed:nil];
            
            [self.bottomInforImageView sd_setImageWithURL:[NSURL URLWithString:tempModel.coverModel.detail] placeholderImage:kPlaceHolderImage completed:nil];
            
        }
        
    }
    
    self.commentArrowsImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.commentTopImageView.frame.size.width / 2 - 15, 5, 20, 20)];
    self.commentArrowsImageView.userInteractionEnabled = YES;
    self.commentArrowsImageView.image = [UIImage imageNamed:@"down_jiantou"];
    UITapGestureRecognizer *commentArrowsTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapCommentArrowsImageView:)];
    [self.commentArrowsImageView addGestureRecognizer:commentArrowsTap];
    [self.commentTopImageView addSubview:_commentArrowsImageView];

    
    
    
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.commentArrowsImageView.frame.origin.y + self.commentArrowsImageView.frame.size.height + 20 , kScreenWidth, 20)];
    self.titleLabel.font = [UIFont fontWithName:@"FZLTZCHJW--GB1-0" size:17];
    self.titleLabel.textColor = [UIColor whiteColor];
    self.titleLabel.textAlignment = 1;
    [self.commentTopImageView addSubview:_titleLabel];

    self.talkLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.titleLabel.frame.origin.y + self.titleLabel.frame.size.height + 15, kScreenWidth, 20)];
    self.talkLabel.font = [UIFont fontWithName:@"FZLTZCHJW--GB1-0" size:13];
    self.talkLabel.textAlignment = 1;
    self.talkLabel.textColor = [UIColor whiteColor];
    [self.commentTopImageView addSubview:_talkLabel];
    
    self.writeLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, self.commentTopImageView.frame.size.height / 3 * 2 - 50, kScreenWidth - 20, 50)];
    self.writeLabel.font = [UIFont fontWithName:@"FZLTXIHJW--GB1-0" size:13];
    self.writeLabel.layer.borderColor = [UIColor whiteColor].CGColor;
    self.writeLabel.layer.borderWidth = 1.0;
    self.writeLabel.textAlignment = 1;
    self.writeLabel.textColor = [UIColor whiteColor];
    [self.commentTopImageView addSubview:_writeLabel];
    
    
    //初始化tableView
    self.commentTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.commentTopImageView.frame.size.height, kScreenWidth, self.coverTopImageView.frame.size.height) style:UITableViewStylePlain];
    self.commentTableView.showsVerticalScrollIndicator = NO;
    self.commentTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.commentTableView.delegate = self;
    self.commentTableView.dataSource = self;
    self.commentTableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_commentTableView];
    
    
    //注册cell:
    
    [self.commentTableView registerClass:[QYXCommentTableViewCell class] forCellReuseIdentifier:@"commentTableViewCell"];
    //定时器:
//    
//    [NSTimer scheduledTimerWithTimeInterval:9 target:self selector:@selector(timeAction) userInfo:nil repeats:YES];
    
    
}


- (void)getCommentData

{
    NSString *urlString = [NSString stringWithFormat:@"http://baobab.wandoujia.com/api/v1/replies/video?_f=iphone&id=%ld&net=wifi&num=20&u=aafd829df8ff2d571a778ed3cca27eb68b16bd7d&v=2.2.0&vc=721", self.commentId];
    [QYXNetTool getNetWithUrl:urlString urlBody:nil header:nil response:QYXJSON success:^(id result) {
        
        //开始解析数据:
        self.commentDataSource = [NSMutableArray array];
        
        NSMutableDictionary *qyxDic = (NSMutableDictionary *)result;
        
        NSArray *array = [NSArray arrayWithArray:[qyxDic objectForKey:@"replyList"]];
        
        for (NSDictionary *dic in array) {
            
            QYXTopRankingModel *topRankingModel = [[QYXTopRankingModel alloc] initWithDic:dic];
            
            [self.commentDataSource addObject:topRankingModel];
            
        }
        
        if (self.commentDataSource.count != 0) {
            self.talkLabel.text = [NSString stringWithFormat:@"- %@ 条评论 -",[qyxDic objectForKey:@"total"]];
            self.writeLabel.text = @"写下你的评论";
           
        }
        [self.commentTableView reloadData];
        
    } failure:^(NSError *error) {
        
    }];
    
}

#pragma mark -- 定时器图片缩放
- (void)timeAction

{
    [UIView animateWithDuration:5 animations:^{
        
        self.commentTopImageView.transform = CGAffineTransformMakeScale(1.2, 1.2);
        
        
    } completion:^(BOOL finished) {
        
        [UIView animateWithDuration:4 animations:^{
            
            self.commentTopImageView.transform = CGAffineTransformIdentity;
            
        } completion:^(BOOL finished) {
            
            
            
        }];
        
        
    }];

}


#pragma mark -- 分栏点击方法:

- (void)arrowTuringAction
{
    
    [UIView animateWithDuration:0.3 animations:^{
        
        self.topArrowImageView.transform = CGAffineTransformRotate(self.topArrowImageView.transform, M_PI / 2);
    } completion:^(BOOL finished) {
        
    }];
}

#pragma mark --  返回箭头:

- (void)tapCommentArrowsImageView:(UITapGestureRecognizer *)sender
{
    CATransition *transition = [CATransition animation];
    transition.duration = 0.5f;
    transition.delegate = self;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionFade;
    transition.subtype = kCATransitionFromTop;
    [self.navigationController.view.layer addAnimation:transition forKey:nil];
    [self.navigationController popViewControllerAnimated:NO];
    
    
}




#pragma mark -- 签协议必须实现的方法:


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{
    
    return self.commentDataSource.count;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{
    QYXTopRankingModel *commentModel = [self.commentDataSource objectAtIndex:indexPath.row];
    
    QYXCommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"commentTableViewCell"];
    
    cell.selectionStyle =  UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor clearColor];
    cell.model = commentModel;
    self.titleLabel.text = commentModel.videoTitle;
 
    return cell;
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{
    return kScreenHeight / 2 / 3;
    
}






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
